package com.example.gallery;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;

public class DirectoryActivity extends Activity{
	
	private String[] itemNames;
	private String rootPath;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_directory);
		
		Intent intent = getIntent();
		rootPath = intent.getStringExtra("assetsPath");
		if(rootPath==null)
		{
			rootPath = "gallery";
		}
		
		try {
			itemNames = getAssets().list(rootPath);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("ItemNames:" + itemNames.toString());
		
		for(int i =0  ; i < itemNames.length ; i ++)
		{
			System.out.println(itemNames[i]);
		}
		
	
		TableLayout tableLayout =  (TableLayout)findViewById(R.id.DirectoryActivity_TableLayout);

		Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);

		int itemCount = itemNames.length;
		System.out.println("itemCount : " + itemCount);
		//셀의 크기 구하기
		int rowCount=1;
		int imageSize = display.getWidth();
		while(true)
		{
			if((imageSize/rowCount) < 300)
			{
				imageSize = imageSize / (rowCount-1);
				rowCount = rowCount-1;
				break;
			}
			rowCount++;
		}
		//셀 구성
		
		int colCount = (itemCount / rowCount) +  (itemCount!=rowCount ? 1 : 0);
	
		int cellCreatedCount = 0;
		System.out.println("ColCount : " + colCount + "RowCount : " + rowCount);
		
		for(int i = 0 ; i < colCount ; i ++)
		{
			TableRow tableRow = new TableRow(this);
			
			tableRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			
			for(int j = 0 ; j < rowCount ; j ++)
			{
				cellCreatedCount++;
				if(cellCreatedCount > itemCount)
				{
					break;
				}
				System.out.println("create");
				final String filename = itemNames[i*rowCount + j];
				View cellView;
				
				
				if(Util.isDirFromAssets(this, rootPath + "/" + filename))
				{
					Button btn = new Button(this);
					btn.setText(filename);
					cellView = btn;
					btn.setOnClickListener(new View.OnClickListener() {
						
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							Intent intent = new Intent(DirectoryActivity.this,DirectoryActivity.class);
							intent.putExtra("assetsPath", rootPath + "/" + filename);
							startActivity(intent);
						}
					});
				}
				else
				{
					ImageView imageView = new ImageView(this);
					imageView.setImageDrawable(new BitmapDrawable(Util.getBitmapFromAssets(this, rootPath + "/" + filename)));
					cellView = imageView;
					imageView.setOnClickListener(new View.OnClickListener() {
						
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							Intent intent = new Intent(DirectoryActivity.this,ImageActivity.class);
							intent.putExtra("assetsPath", rootPath + "/" + filename);
							startActivity(intent);
						}
					});
				}
				tableRow.addView(cellView,imageSize,imageSize);
			}
			tableLayout.addView(tableRow);

		}

		
	}
}
